﻿package core
{
	/**
	 * @version 1.1.0
	 * 
	 * 
	 * @author Kylin_206
	 * 	2013-02-22
	 */
	public class IntVector 
	{
		private var m_row:int;
		private var m_col:int;
		
		public static const EAST:IntVector = new IntVector(0, 1);
		public static const WEST:IntVector = new IntVector(0, -1);
		public static const NORTH:IntVector = new IntVector(-1, 0);
		public static const SOUTH:IntVector = new IntVector(1, 0);
		
		public static const NORTH_EAST:IntVector = new IntVector( -1, 1);
		public static const NORTH_WEST:IntVector = new IntVector( -1, -1);
		public static const SOUTH_EAST:IntVector = new IntVector(1, 1);
		public static const SOUTH_WEST:IntVector = new IntVector(1, -1);
		
		public static const ZERO:IntVector = new IntVector(0, 0);
		
		/**
		 * 请注意，构建的 IntVector对象应该被视为一个常量，不应对其属性进行修改
		 * @param	row
		 * @param	col
		 */
		public function IntVector(row:int,col:int) 
		{
			m_row = row;
			m_col = col;
		}
		
		public function get row():int
		{
			return m_row;
		}
		
		public function get col():int
		{
			return m_col;
		}
		
		/**
		 * 将另一个点的坐标添加到此点的坐标以创建一个新点
		 * @param gv
		 * @return 
		 * 
		 */		
		public function add(gv:IntVector):IntVector
		{
			return new IntVector(this.m_row + gv.m_row, this.m_col + gv.m_col);
		}
		
		/**
		 * 从此点的坐标中减去另一个点的坐标以创建一个新点
		 * @param	gv
		 * @return
		 */
		public function subtract(gv:IntVector):IntVector
		{
			return new IntVector(this.m_row - gv.m_row, this.m_col - gv.m_col);
		}
		
		/**
		 * 让当前点离目标点更近distance格，如果实际距离小于此点，那么会与此点重合
		 * @param	gv 目标点
		 * @param	distance 要拉近的距离
		 * @return
		 */
		public function minusDistance(gv:IntVector,$distance:int):IntVector
		{
			if (distance(this, gv) < $distance)
				return new IntVector(gv.row, gv.col);
			var myRow:int = m_row;
			var myCol:int = m_col;
			var dicX:int = gv.row - myRow > 0?1:-1;
			var dicY:int = gv.col - myCol > 0?1: -1;
			var rd:Number;
			while ($distance)
			{
				rd = Math.random();
				if (rd > .5)
				{
					if (Math.abs(gv.row - myRow) > Math.abs(gv.col - myCol))
						myRow += dicX;
					else
						myCol += dicY;
				}
				else
				{
					if (Math.abs(gv.row - myRow) >= Math.abs(gv.col - myCol))
						myRow += dicX;
					else
						myCol += dicY;
				}
				$distance --;
			}
			return new IntVector(myRow,myCol);
		}
		
		/**
		 * 取得一个倒转后的IntVector
		 * @return
		 */
		public function reverse():IntVector
		{
			return new IntVector(this.m_row * -1, this.m_col * -1);
		}
		
		/**
		 * 确定两个点是否相同
		 * @param	gv
		 * @return
		 */
		public function equals(gv:IntVector):Boolean
		{
			if (null == gv) return false;
			if(this.m_col == gv.m_col && this.m_row == gv.m_row) return true;
			
			return false;
		}
		
		/**
		 * 返回两点间的格数
		 * @return
		 */
		public static function distance (gv0:IntVector, gv1:IntVector):int
		{
			return Math.abs(gv1.m_row - gv0.m_row) + Math.abs(gv1.m_col - gv0.m_col);
		}
		
		public function clone():IntVector
		{
			return new IntVector(m_row, m_col);
		}
		
		public function toString():String
		{
			return "[IV:" + this.m_row +"," + this.m_col+"]";
		}
	}
	
}